<?php

namespace app\admin\model\chase;

use think\Model;


class UserVip extends Model
{

    

    

    // 表名
    protected $table = 'user_vip';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function getOnlineList()
    {
        return ['0' => __('Online 0'), '1' => __('Online 1')];
    }

    public function getByName($name)
    {
        return $this->where('name', $name)->find();
    }
    

    public function equity()
    {
        return $this->belongsToMany(UserEquity::class, 'user_vip_equity', 'equity_id', 'user_vip_id');
    }

    public function coupon()
    {
        return $this->belongsToMany(Coupon::class, 'user_vip_coupon', 'coupon_id', 'user_vip_id');
    }

    public function user()
    {
        return $this->hasMany(User::class, 'vip', 'name');
    }

    public static function getUserVip()
    {
        $vip = self::getVips();
        foreach ($vip as $val) {
            $val['user_money'] = User::where('vip', $val['name'])->sum('money');
            $val['user_num'] = User::where('vip', $val['name'])->count();
        }
        return $vip;
    }

    public static function getVips()
    {
        return self::where('online', 1)->order('name asc')->select();
    }

    /**
     * 获取会员等级
     */
    public static function getNotByName($name)
    {
        return self::where('online', 1)->whereNotIn('name', [$name])->order('name asc')->select();
    }

}
